/**
 * @Author：ming
 * @Date：2023/10/2 3:18 AM
 * @Filename：Lc105
 */

/**
 * @author ming
 */
public class Lc105 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return build(preorder,inorder,0,inorder.length-1,0);
    }

    public TreeNode build(int[] preorder,int[] inorder,int l,int r,int index ){
        if(index>inorder.length-1){
            return null;
        }
        TreeNode treeNode = new TreeNode();
        int root = preorder[index];
        treeNode.val =root;
        int left = 0;
        int right =0;
        for (int i = 0; i < inorder.length; i++) {
            if(inorder[i]==root){
                right  = i-1;
                left = i+1;
                break;
            }
        }


        treeNode.left=build(preorder,inorder,l,right,++index);
        treeNode.right=build(preorder,inorder,left,r,++index);
        return treeNode;

    }
}
